package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.cookie.SM;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 目标:
 *
 * @author YUE
 * @date 2024/6/12 15:56
 */
@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    /*
      新增套餐
     */
    @Transactional
    @Override
    public void add(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmeal.setStatus(StatusConstant.DISABLE);


        setmealMapper.saveId(setmeal);

        // 获取回显 的 主键id
        Long setmealId = setmeal.getId();

        //根据id 添加套餐菜品
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            // 根据套餐的id 添加菜品  所要将主键回显的id 赋值给 套餐菜品表的套餐id
            setmealDish.setSetmealId(setmealId);
        }

        setmealDishMapper.insterBatch(setmealDishes);
    }

    /*
      套餐分页查询
     */
    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.page(setmealPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }


    /*
      批量删除
     */
    @Override
    @Transactional
    public void delete(List<Long> ids) {
        // 删除套餐表 和 菜品表
        // 判断状态 起售不可删
        for (Long id : ids) {
            Setmeal setmeal = setmealMapper.getById(id);
            if (setmeal.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);//起售中的套餐不能删除
            }
        }
        for (Long id : ids) {
            setmealMapper.deleteSetmeal(id); // 根据主键删除套餐表
            setmealDishMapper.deletesetmealDish(id); // 根据套餐id删除菜品表
        }
    }

    /*
      根据id查询套餐
     */
    @Override
    public SetmealVO getByIdSetmeal(Long id) {
        Setmeal setmeal = setmealMapper.getById(id);
        // 根据id查询菜品
        List<SetmealDish> setmealDishes = setmealDishMapper.getByIdDish(id);

        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);
        setmealVO.setSetmealDishes(setmealDishes);

        return setmealVO;
    }

    /*
      修改套餐数据
     */
    @Transactional
    @Override
    public void update(SetmealDTO setmealDTO) {
        // 修改两个表 套餐 菜品
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);

        setmealMapper.update(setmeal);

        // 修改菜品表 先删 后添加
        setmealDishMapper.deletesetmealDish(setmealDTO.getId());

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null && setmealDishes.size() > 0) {
            for (SetmealDish setmealDish : setmealDishes) {
                //
                setmealDish.setSetmealId(setmealDTO.getId());
            }
        }
        setmealDishMapper.insterBatch(setmealDishes);

    }

    /*
      启用禁用
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        // 起售套餐时 判断套餐内是否有停手菜品 有停售菜品提示"...."
        // 如果套餐起售
        if(status == StatusConstant.ENABLE){
            // 根据套餐id查相对菜品
           List<Dish> dishes = dishMapper.getBySetmealId(id);
           if (dishes != null && dishes.size() > 0){
               for (Dish dish : dishes) {
                   if (StatusConstant.DISABLE == dish.getStatus()){
                       throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                   }
               }
           }
        }

        Setmeal setmeal = new Setmeal().builder()
                .id(id)
                .status(status)
                .build();
        setmealMapper.update(setmeal);
    }

    /**
     * 条件查询
     * @param setmeal
     * 根据分类id查询套餐
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据套餐id查询包含的菜品
     * @param id
     * @return
     */
    @Override
    public List<DishItemVO> getDishItemById(Long id) {
      List<DishItemVO> dishItemVO =  setmealMapper.getIdByDish(id);
        return dishItemVO;
    }
}
